package Array;

public class trainList {
    private int size;
    private Node head;
    public void addFirst(int index) {
        Node node = new Node(index);
        if(head == null){
            head = node;
        }else{
            node.next = head;
            head = node;
        }
        size++;
    }
    public void addIndex(int index,int newVal){
        if(head == null){
            addFirst(index);
        }
        if(0>index || index >size){
            System.err.println("输入有误");
            return;
        }
        Node node = new Node(newVal);
        Node prev = head;
        for (int i = 0; i < index - 1; i++) {
            prev = prev.next;
        }
        node.next = prev.next;
        prev.next = node;
        size++;
    }
    public void addLast(int newVal){
        Node node = new Node(newVal);
        Node prev = head;
        for (int i = 0; i < size - 1; i++) {
            prev = prev.next;
        }
        prev.next = node;
        size++;
    }
    public String toString(){
        String ret = "";
        Node node = head;
        while (node != null){
            ret += node.val;
            ret += "->";
            node = node.next;
        }
        ret += "NULL";
        return ret;
    }
    public void removeFirst(){
        head = head.next;
        size--;
    }
    public void removeIndex(int index){
        if(0>index || index >size){
            System.err.println("输入有误");
            return;
        }
        Node prev = head;
        Node cur = head;
        for (int i = 0; i < index - 2; i++) {
            prev = prev.next;
        }
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        prev.next = cur.next;
        cur.next = null;
        size--;
    }
    public void removeValOnce(int val){
        if(head ==null){
            System.err.println("链表为空!!!");
            return;
        }
        if(head != null && head.val == val){
            Node prev = head;
            head = head.next;
            prev.next = null;
            size--;
        }else{
            Node prev = head;
            while(prev.next != null){
                if(prev.next.val == val){
                    Node cur = prev.next;
                    prev.next = cur.next;
                    cur.next = null;
                    size--;
                    return;
                }else{
                    prev = prev.next;
                }
            }
        }
    }
    public void removeValAll(int val){
        while(head != null && head.val == val){
            head = head.next;
            size--;
        }
        if(head == null){
            return;
        }else{
            Node prev = head;
            while(prev.next != null){
                if(prev.next.val == val){
                    Node cur = prev.next;
                    // 删除cur
                    prev.next = cur.next;
                    cur.next = null;
                    size --;
                }else{
                    prev = prev.next;
                }
            }
        }
    }
    public void setIndex(int index,int newVal){
        if(0>index || index >size){
            System.err.println("输入有误");
            return;
        }
        Node prev = head;
        for (int i = 0; i < index - 1; i++) {
            prev = prev.next;
        }
        prev.val = newVal;
    }
    public void getIndex(int index){
        if(0>index || index >size){
            System.err.println("输入有误");
            return;
        }
        Node prev = head;
        for (int i = 0; i < index - 1; i++) {
            prev = prev.next;
        }
        System.out.println(prev.val);
    }
}
class Node {
    int val;
    Node next;

    public Node(int val) {
        this.val = val;
    }
}